<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
	//登录
    public function index(){
    	if($_GET[op]=='logout'){
    		session('user',null);
    	}
    	if($_GET[op]=='check'){
	    	if(md5($_POST[userpassword])==D('Admin')->getField("password")){
	    		session('user','admin');
	    		$this->display(":membermanager");
	    		exit();
	    	}else{
	    		$this->error("账户或密码输入有误，请重新输入");
	    	}
    	}
		$this->display(":login");
    }
    public function forget(){
    	$this->display(":forget");
    }
    //注册用户统计
    public function financeaccount(){
    	isLogin();
  
    	$this->display(":financeaccount");
    }
    //用户管理
    public function membermanager(){
		isLogin();
    	$op = $_GET[op];
		if($op=="select"){
			$name = $_POST['name'];
			$phone = $_POST['phone'];
			$status = $_POST['status'];
			$vip = $_POST['vip'];
			$usertype = $_POST['usertype'];
			$serialnumber = $_POST['serialnumber'];
			if($name||$phone||$status||$vip||$usertype||$serialnumber){
				$listdata = D('user__phone')->field('serialnumber,name,phone,status')
				->table('58__user__phone')
				->union('SELECT serialnumber,name,phone,status FROM 58__user__qq')
				->union('SELECT serialnumber,name,phone,status FROM 58__user__sina')
				->union('SELECT serialnumber,name,phone,status FROM 58__user__weixin')
				->union('SELECT serialnumber,name,phone,status FROM 58__user__paybao')
				->select();
			}
		}
		if($op=="detail"){
			$serialnumber = $_GET['serialnumber'];
			$usertype = D('user')->where("serialnumber='%s'",$serialnumber)->getField('usertype');
			if($usertype==1){
				$listdata = D('user')->join('LEFT JOIN __USERPHONE__ ON __USER__.serialnumber = __USERPHONE__.phoneserialnumber')->where("serialnumber='%s'",$serialnumber)->select();
			}elseif($usertype==2){
				$listdata = D('user')->join('LEFT JOIN __USERQQ__ ON __USER__.serialnumber = __USERQQ__.qqserialnumber')->where("serialnumber='%s'",$serialnumber)->select();
			}elseif($usertype==3){
				$listdata = D('user')->join('LEFT JOIN __USERWEIXIN__ ON __USER__.serialnumber = __USERWEIXIN__.weixinserialnumber')->where("serialnumber='%s'",$serialnumber)->select();
			}elseif($usertype==4){
				$listdata = D('user')->join('LEFT JOIN __USERPAYBAO__ ON __USER__.serialnumber = __USERPAYBAO__.paybaoserialnumber')->where("serialnumber='%s'",$serialnumber)->select();
			}elseif($usertype==5){
				$listdata = D('user')->join('LEFT JOIN __USERSINA__ ON __USER__.serialnumber = __USERSINA__.sinaserialnumber')->where("serialnumber='%s'",$serialnumber)->select();
			}
			$this->assign('usertype',$usertype);
			$this->assign('listdata',$listdata[0]);
			$this->display(":membermanagerdetail");
			exit();
		}
		$listdata = D('user')->select();
		$this->assign('listdata',$listdata);
    	$this->display(":membermanager");
    }
    //相册管理
    public function photoalbummanage(){
    	isLogin();
		if($_GET["op"]=="album"){
			$this->display(":photoalbummanage");
			exit();
		}
		if($_GET["op"]=="theme"){
			$datalist = D("theme")->select();
			$this->assign("datalist",$datalist);
			$this->display(":photoaltheme");
			exit();
		}
		if($_GET["op"]=="themeadd"){

			if($_GET["do"]=="themeinsert"){
				$data[themename] = $_POST[themename];
				if(!empty($_FILES[themefile][name])){
					$upload = new \Think\Upload();
					$upload->maxSize   =     3145728 ;// 设置附件上传大小
					$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
					$upload->rootPath  =     MODULE_PATH.'Uploads/'; // 设置附件上传根目录
					$upload->savePath  =     ''; // 设置附件上传（子）目录
					// 上传文件
					$info   =   $upload->upload();
					if(!$info) {// 上传错误提示错误信息
						$this->error($upload->getError());
					}else{// 上传成功
						foreach($info as $file){
							$memberimgurl=__ROOT__.substr(MODULE_PATH,1).'Uploads/'.$file['savepath'].$file['savename'];
						}
						$data['photoframeimgurl']=$memberimgurl;
					}
				}
				D("theme")->data($data)->add();
				header('Location:?m=Admin&c=Index&a=photoalbummanage&op=theme');
				exit();
			}
			$this->display(":themeadd");
			exit();
		}
		if($_GET["op"]=="themesetting"){
			$themeid = $_GET["themeid"];
			$this->assign("themeid",$themeid);
			if($_GET["do"]=="themeupdate"){
				$data[themename] = $_POST[themename];
				if(!empty($_FILES[themefile][name])){
					$upload = new \Think\Upload();
					$upload->maxSize   =     3145728 ;// 设置附件上传大小
					$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
					$upload->rootPath  =     MODULE_PATH.'Uploads/'; // 设置附件上传根目录
					$upload->savePath  =     ''; // 设置附件上传（子）目录
					// 上传文件
					$info   =   $upload->upload();
					if(!$info) {// 上传错误提示错误信息
						$this->error($upload->getError());
					}else{// 上传成功
						foreach($info as $file){
							$memberimgurl=__ROOT__.substr(MODULE_PATH,1).'Uploads/'.$file['savepath'].$file['savename'];
						}
						$data['photoframeimgurl']=$memberimgurl;
					}
				}
				D("theme")->where("id='%d'",$themeid)->save($data);
				
				header('Location:?m=Admin&c=Index&a=photoalbummanage&op=theme');
				exit();
			}
			$this->assign("data",D("theme")->where("id=$themeid")->select());
			$this->display(":themesetting");
			exit();
		}
		if($_GET["op"]=="themedelete"){
			$themeid = $_GET["themeid"];
			D("theme")->where("id=$themeid")->delete();
			header('Location:?m=Admin&c=Index&a=photoalbummanage&op=theme');
			exit();
		}
		$datalist = D("theme")->select();
		$this->assign("datalist",$datalist);
    	$this->display(":photoalbummanage");
    }
    //系统设置
    public function setting(){
    	isLogin();
    	$op = $_GET[op];
    	if($op=="qiniu"){
    		$do = $_GET['do'];
    		if($do=="update"){
    			$apidata = array(
    					'qqkey'=>$_POST['qqkey'],
    					'qqsecret'=>$_POST['qqsecret'],
    					'sinakey'=>$_POST['sinakey'],
    					'sinasecret'=>$_POST['sinasecret'],
    					'alipaykey'=>$_POST['alipaykey'],
    					'alipaysecret'=>$_POST['alipaysecret'],
    					'qiniukey'=>$_POST['qiniukey'],
    					'qiniusecret'=>$_POST['qiniusecret'],
    					'bucket'=>$_POST['bucket'],
    					'qiniuurl'=>$_POST['qiniuurl'],
    					'qiniustatus'=>$_POST['qiniustatus']
    			);
    			D("admin")->where('id=1')->setField($apidata);
    			$this->success("修改成功");
    			exit();
    		}
    		$listdata = D("admin")->where('id=1')->select();
    		$this->assign("listdata",$listdata);
    		$this->display(":settingthirdparty");
    		exit();
    	}
    	if($op=="vip"){
    		$this->display(":settingvip");
    		exit();
    	}
    	if($op=="notice"){
    		$this->display(":settingnotice");
    		exit();
    	}
    	if($op=="other"){
    		$this->display(":settingother");
    		exit();
    	}
    	
    	/*
    	 * 轮播
    	 */
    	if($op=="lunbo"){
    		$lunbolist = D("lunbo")->select();
    		$this->assign('lunbolist',$lunbolist);
    		$this->display(":settinglunbo");
    		exit();
    	}
    	if($op=="lunboadd"){
    		$do = $_GET['do'];
    		if($do=="insert"){
	    		$data[lunboorder]=$_POST["displayorder"];
	    		$data[title]=$_POST["title"];
	    		$data[status]=$_POST["enabled"];
	    		$data[clickurl]=$_POST["link"];
	    		if(!$_POST["displayorder"]){
	    			$this->error("排序号不能为空");
	    		}
	    		if(D("lunbo")->where("lunboorder=$data[lunboorder]")->select()!=array()){
	    			$this->error("排列序号已存在");
	    		}
	    		if(!empty($_FILES[myfile][name])){
	    			$upload = new \Think\Upload();
	    			$upload->maxSize   =     3145728 ;// 设置附件上传大小
	    			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    			$upload->rootPath  =     MODULE_PATH.'/Uploads/'; // 设置附件上传根目录
	    			$upload->savePath  =     ''; // 设置附件上传（子）目录
	    			// 上传文件
	    			$info   =   $upload->upload();
	    			if(!$info) {// 上传错误提示错误信息
	    				$this->error($upload->getError());
	    			}else{// 上传成功
	    				foreach($info as $file){
	    					$data[imgurl]=__ROOT__.substr(MODULE_PATH,1).'Uploads/'.$file['savepath'].$file['savename'];
	    				}
	    			}
	    		}
	    		
	    		D("lunbo")->data($data)->add();
				$this->success('插入成功', U('Admin/Index/setting?op=lunbo'));
	    		exit();
    		}
    		$this->display(":settinglunboadd");
    		exit();
    	}
    	if($op=="lunbodetail"){
    		$id = $_GET["id"];
    		$do = $_GET["do"];

    		$lunbolist = D("lunbo")->where("id=$id")->select();
    		$this->assign('lunbolist',$lunbolist);
    		$this->assign('id',$id);
    		if($do=="updates"){
    			$data[lunboorder]=$_POST["displayorder"];
    			$data[title]=$_POST["title"];
    			$data[status]=$_POST["enabled"];
    			$data[clickurl]=$_POST["link"];
    			if(!$data[lunboorder]){
    				$this->error("排列序号不允许为空");
    			}
    			if(D("lunbo")->where("lunboorder=$data[lunboorder]")->select()!=array()&&$data[lunboorder]!=D("lunbo")->where("id=$id")->getField('lunboorder')){
    				$this->error("排列序号已存在");
    			}
    			
    			
    			if(!empty($_FILES[myfile][name])){
	    			$upload = new \Think\Upload();
	    			$upload->maxSize   =     3145728 ;// 设置附件上传大小
	    			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    			$upload->rootPath  =     MODULE_PATH.'/Uploads/'; // 设置附件上传根目录
	    			$upload->savePath  =     ''; // 设置附件上传（子）目录
	    			// 上传文件
	    			$info   =   $upload->upload();
	    			if(!$info) {// 上传错误提示错误信息
	    				$this->error($upload->getError());
	    			}else{// 上传成功
	    				foreach($info as $file){
	    					$data[imgurl]=__ROOT__.substr(MODULE_PATH,1).'Uploads/'.$file['savepath'].$file['savename'];
	    				}
	    			}
    			}

    			if(D("lunbo")->where("id=$id")->data($data)->save()){
    				$this->success('修改成功', U('Admin/Index/setting?op=lunbodetail&id='.$id));
    			}else{
    				if($data[lunboorder]==D("lunbo")->where("id=$id")->getField('lunboorder')){
    					$this->error("未有修改数据项");
    				}
    				$this->error("修改失败，请检查输入");
    			}
    			exit();
    		}
    		$this->display(":settinglunbodetail");
    		exit();
    	}
    	if($op=="lunbodelete"){
    		$id = $_GET["id"];
    		D("lunbo")->where("id=$id")->delete();
    		$this->success('删除成功', U('Admin/Index/setting?op=lunbo'));
    		exit();
    	}
    	
    	/*
    	 * 广告位
    	 */
    	if($op=="ad"){
    		$datalist = D("ad")->select();
    		$homenotice = D("admin")->getField("homenotice");
    		$this->assign("homenotice",$homenotice);
    		$this->assign("datalist",$datalist);
    		$this->display(":settingad");
    		exit();
    	}
    	if($op=="order"){
    		$adid = $_POST['adid'];
 
    		$adorder = $_POST['adorder'];
			if(count($adorder)!=count(array_unique($adorder))){
				$this->error("排序序号存在重复，请重新输入",U("Admin/Index/setting")."&op=ad");
			}
			
    		for($i=0;$i<count($adid);$i++){
    			D("ad")->where("id='%d'",$adid[$i])->setField("adorder",$adorder[$i]);
    		}
    		$this->success("排序修改成功",U("Admin/Index/setting")."&op=ad");
    		exit();
    	}
    	if($op=="adadd"){
    		
    		$do = $_GET['do'];
    		if($do=="insert"){
    			
    			if(!empty($_FILES[adfile][name])||!empty($_FILES[writerimgfile][name])){
    				$upload = new \Think\Upload();
    				$upload->maxSize   =     3145728 ;// 设置附件上传大小
    				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    				$upload->rootPath  =     MODULE_PATH.'/Uploads/'; // 设置附件上传根目录
    				$upload->savePath  =     ''; // 设置附件上传（子）目录
    				// 上传文件
    				$info   =   $upload->upload();
    				if(!$info) {// 上传错误提示错误信息
    					$this->error($upload->getError());
    				}else{// 上传成功
    					foreach($info as $key=>$file){
    						$data[imgurl][$key]=__ROOT__.substr(MODULE_PATH,1).'Uploads/'.$file['savepath'].$file['savename'];
    						
    					}
//     					file_put_contents(MODULE_PATH."log.txt","\n old:".json_encode($data[imgurl][adfile]),FILE_APPEND);
//     					 exit();
    				}
    			}else{
    				$this->error("广告图片和作者头像不能为空");
    			}
    			
    			if(D("ad")->create()){
    				D("ad")->add();
    				$imgdata = array('imgurl'=>$data[imgurl][adfile],'writerimgurl'=>$data[imgurl][writerimgfile]);
    				D("ad")->where("adorder='%d'",$_POST['adorders'])->setField($imgdata);
    				$this->success("插入成功",U('Admin/Index/setting?op=ad'));
    				exit();
    			}else{
    				$this->error(D("ad")->getError());
    			}
    			
    			
    			
    		}
    		$this->display(":settingadadd");
    		exit();
    	}
    	if($op=="adupdate"){
    		$id = $_GET["id"];
    		$do = $_GET["do"];
    		if($do=="update"){
    			if(!empty($_FILES[adfile][name])||!empty($_FILES[writerimgfile][name])){
    				$upload = new \Think\Upload();
    				$upload->maxSize   =     3145728 ;// 设置附件上传大小
    				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    				$upload->rootPath  =     MODULE_PATH.'/Uploads/'; // 设置附件上传根目录
    				$upload->savePath  =     ''; // 设置附件上传（子）目录
    				// 上传文件
    				$info   =   $upload->upload();
    				if(!$info) {// 上传错误提示错误信息
    					$this->error($upload->getError());
    				}else{// 上传成功
    					foreach($info as $key=>$file){
    						$data[imgurl][$key]=__ROOT__.substr(MODULE_PATH,1).'Uploads/'.$file['savepath'].$file['savename'];
    			
    					}
    				}
    			}
    			if(D("ad")->create(I('post.'),2)){
    				D("ad")->where("id=$id")->save();
    				if($data[imgurl][adfile]&&$data[imgurl][writerimgfile]){
    					$imgdata = array('imgurl'=>$data[imgurl][adfile],'writerimgurl'=>$data[imgurl][writerimgfile]);
    				}elseif($data[imgurl][adfile]&&!$data[imgurl][writerimgfile]){
    					$imgdata = array('imgurl'=>$data[imgurl][adfile]);
    				}elseif(!$data[imgurl][adfile]&&$data[imgurl][writerimgfile]){
    					$imgdata = array('writerimgurl'=>$data[imgurl][writerimgfile]);	
    				}
    				D("ad")->where("id='%d'",$id)->setField($imgdata);
    				$this->success("修改成功",U('Admin/Index/setting?op=adupdate&id=').$id);
    				exit();
    			}else{
    				$this->error(D("ad")->getError());
    			}
    		}
    		$datalist = D("ad")->where("id=$id")->select();
    		$this->assign("datalist",$datalist);
    		$this->display(":settingadupdate");
    		exit();
    	}
    	if($op=="addelete"){
			$id=$_GET['id'];
			if(D("ad")->where("id=$id")->delete()){
				$this->success("删除成功");
			}
    		exit();
    	}
    	if($op=="noticehome"){
    		if(D("admin")->where("id=1")->setField("homenotice",$_POST["noticemessage"])){
    			$this->success("修改成功",U('Admin/Index/setting?op=ad'));
    		}else{
    			$this->error(D("admin")->getError());
    		}

    		exit();
    	}
    	$this->display(":settingthirdparty");
    }
    /*
     * 公告栏
     */
    public function noticeboard(){
    	isLogin();
    	$this->display(":noticeboard");
    }
}